(function (win, dom) {
    function Keyboard(option) {
        return new Keyboard.prototype.init(option);
    }
    Keyboard.prototype = {
        constructor:Keyboard,
        init:function (opt) {
        //    存储输入的值
            this.result = '';
            this.option = opt;
        //    创建ul
            this._ul = document.createElement('ul');
        //    判断是否传入了for
            this.showContainer = opt.for ? document.getElementById(opt.for) : console.warn('the prop "for" of the config object must be the string of id');
        //    显示的是否是明文， 通过input[type = password]
            this.option.clearText||(this.showContainer.type = 'password');
        //    初始化键盘
            this.render();
        },
        render:function () {
        //    创建文档碎片
            var doc = document.createDocumentFragment();
            this._ul.className = 'keyboard-container';
        //    循环创建li标签
            for(var i = 0; i < 12; i++){
                var _ele = document.createElement('li');
                if(i === 9){
                    _ele.className = 'keyboard-key';
                    _ele.innerHTML = '#';
                }else if(i === 10){
                    _ele.className = 'keyboard-key';
                    _ele.innerHTML = '0';
                }else if(i === 11){
                    _ele.className = 'keyboard-key keyboard-del';
                    _ele.innerHTML = '<';
                }else {
                    _ele.className = 'keyboard-key';
                    _ele.innerHTML = i + 1 + '';
                }
                //循环插入li标签
                this._ul.appendChild(_ele);
            }
            doc.appendChild(this._ul);
            document.body.appendChild(doc);
            this.bindEv();
        },
        bindEv:function () {
            var _this = this;
        //    事件委托
            this._ul.onclick = function (event) {
                if(event.srcElement.className === 'keyboard-key'){
                    //输入数字的长度小于所传的长度
                    if(_this.result.length < +_this.option.length){
                        _this.result += event.srcElement.innerHTML + '';
                        //将所得数字添加到input里
                        _this.keyDown();
                        return;
                    }
                    _this.option.valiReg.test(_this.result)?_this.option.validSuccess.call(_this):_this.option.validError.call(_this);
                }else {
                //    点击删除按钮时
                    _this.result = _this.result.slice(0, _this.result.length - 1);
                    _this.keyDown();
                }

            };
        //    关闭input的默认弹出键盘事件
            this.showContainer.onfocus = function () {
                _this.show();
                this.blur();
            }
        },
    //    实时添加数据
        keyDown:function () {
            this.showContainer.value = this.result;
        },
    //    隐藏键盘
        hide:function () {
            this._ul.style.cssText = 'bottom:-80vh;'
        },
    //    键盘显示
        show:function () {
            this._ul.style.cssText = 'bottom:0;'
        },
    //    抛出数据
        getVal:function () {
            var result = this.result;
            return result.length === this.option.length && result;
        }
    };
    Keyboard.prototype.init.prototype = Keyboard.prototype;
    Keyboard.prototype.init.constructor = Keyboard.prototype.init;
    win['Keyboard'] = Keyboard;
})(window, document);